import mysql from "mysql";

const dbConfig = {
    host: "127.0.0.1", // 本地
    port: 3306, // 端口DDDD
    user: "root", // 用户
    password: "root", // 密码
    database: "2203a", //数据名称
};

const pool = mysql.createPool(dbConfig);

const query = (sql: string) => {
    return new Promise<any>((resolve, reject) => {
        pool.getConnection((error: any, connection: any) => {
            if (error) {
                reject(error);
            } else {
                connection.query(sql, (error: any, results: any) => {
                    if (error) {
                        reject(error);
                    } else {
                        resolve(results);
                    }
                    connection.release(); // 释放该链接，把该链接放回池里供其他人使用
                });
            }
        });
    });
};

export default query;